package crawer.ui;

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.event.*;

class MyPipe implements Runnable {
	private JTextArea textarea;

	private PipedInputStream pis = new PipedInputStream();

	private PipedOutputStream pos;

	private BufferedReader reader = new BufferedReader(new InputStreamReader(
			pis));

	private Thread thread = null;

	public MyPipe(JTextArea ta) throws IOException {
		textarea = ta;
		pos = new PipedOutputStream(pis);
		thread = new Thread(this);
		thread.start();
	}

	public PipedOutputStream getOut() {
		return pos;
	}

	public void run() {
		String line = null;
		while (true) {
			try {
				line = reader.readLine();
				System.err.println(line);
			} catch (IOException ioe) {
				break;
			}

			if (line == null) {
				break;
			} else {
				final String s = line;
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						textarea.append(s);
						textarea.append(System.getProperty("line.separator",
								"\n\r"));
					}
				});
			}
		}
	}

	public void close() {
		thread = null;
	}
}
